﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAREAB15 : DocumentBase
    {
        #region-------------Properties-----------------------
        DataRow LastRow = null;
        DataSet SetMap = null;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        YCAREAB15Data m_Data, m_DataX;
        public DataSet dsParamaters;
        YCAREAB15Report m_rpt;
        DataTable tbUser = null;
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetMaster
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetMaster");
            }
        }

        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        CollectionViewSource vwABPOS_MAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS_MAT");
            }
        }
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }

        CollectionViewSource vwYARNMatrix
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwYARNMatrix");
            }
        }
        CollectionViewSource vwFABMatrix
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFABMatrix");
            }
        }
        CollectionViewSource vwFSSTAMM
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFSSTAMM");
            }
        }

        DataRow dwFSSTAMM
        {
            get
            {
                if (vwFSSTAMM.View == null)
                    return null;
                if ((DataRowView)vwFSSTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFSSTAMM.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwSFSGROE
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwSFSGROE");
            }
        }
        DataRow dwSFSGROE
        {
            get
            {
                if (vwSFSGROE.View == null)
                    return null;
                if ((DataRowView)vwSFSGROE.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwSFSGROE.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwSFSFARBEN
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwSFSFARBEN");
            }
        }
        DataRow dwSFSFARBEN
        {
            get
            {
                if (vwSFSFARBEN.View == null)
                    return null;
                if ((DataRowView)vwSFSFARBEN.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwSFSFARBEN.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwSFSSTAMM
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwSFSSTAMM");
            }
        }
        DataRow dwSFSSTAMM
        {
            get
            {
                if (vwSFSSTAMM.View == null)
                    return null;
                if ((DataRowView)vwSFSSTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwSFSSTAMM.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwMFSSTUELI
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMFSSTUELI");
            }
        }
         
        DataRow dwMFSSTUELI
        {
            get
            {
                if (vwMFSSTUELI.View == null)
                    return null;
                if ((DataRowView)vwMFSSTUELI.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMFSSTUELI.View.CurrentItem).Row;
            }
        }
         
        DataRow dwBOM = null;
        string ART
        {
            get
            {
                return m_Data.Left(txtMAT.Text.ToString() + "                      ", 20);
            }
        }

        string user = "";
        #endregion
        public YCAREAB15()
        {
            InitializeComponent();
            this.Name = "插入新原料";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            tbUser = (DataTable)dicParamater["USERRIGHT"];
            m_Data = new YCAREAB15Data(OracleDB, proxy);
            user = (string)dicParamater["User"];
            txtMAT.Text = "B08F3BK001          ";
            txtFSART.Text = "2411309005";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                vwFSSTAMM.View.CurrentChanged += vwFSSTAMM_CurrentChanged;
                vwMFSSTUELI.View.CurrentChanged += View_CurrentChanged;
                ResetSetInterface();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        void View_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMFSSTUELI == null)
                return;
            if (LastRow == dwMFSSTUELI)
                return;
            LastRow = dwMFSSTUELI;
            try
            { 
                DataTable tbBOM = null;
                if (!SetProd.Tables.Contains("FSSTUELI_EXT"))
                {
                    tbBOM = SetProd.FSSTUELI.Clone();
                    tbBOM.TableName = "FSSTUELI_EXT";
                    SetProd.Merge(tbBOM);
                }
                tbBOM = SetProd.Tables["FSSTUELI_EXT"];
                tbBOM.Clear();
                string art = dwMFSSTUELI["FSSART"].ToString();

                string mart = dwMFSSTUELI["FSSMART"].ToString();
                string filter = "FSSART='" + art + "' and FSSMART='" + mart + "'";
                DataRow[] rows = SetMaster.FSSTUELI.Select(filter);
                if (rows.Length == 0)
                {
                    MessageBox.Show("子物料不存在");
                    return;
                }

                tbBOM.Merge(rows.CopyToDataTable());
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }
                SetMaster.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void vwFSSTAMM_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwFSSTAMM == null)
                    return;
                if (LastRow == dwFSSTAMM)
                    return;
                LastRow = dwFSSTAMM;
                using (new LongOperation(this, "数据更新中"))
                {
                    DataSet SResult = m_Data.GetData(dwFSSTAMM);
                    SetMaster.Clear();
                    SetMaster.Merge(SResult);
                    if (SetProd.Tables.Contains("FSSTUELI_EXT"))
                    {
                        SetProd.Tables["FSSTUELI_EXT"].Clear();
                    }
                
                    if (SResult.Tables.Contains("ABPOS"))
                    {
                        SetProd.ABPOS.Clear();
                        SetProd.Merge(SResult.Tables["ABPOS"]);
                        if (vwABPOS.View != null)
                        {
                            vwABPOS.View.MoveCurrentToFirst();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
     
       
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            DataRow dwTemp = SetData.FSSTAMM.Rows.Add();
            dwTemp["FSMANDANT"] = "00";
            dwTemp["FSART"] = txtFSART.Text.Trim();

            SetProd.AcceptChanges();
            DataSet SResult = m_Data.GetFSList(dwTemp);
            SetData.Clear();
            SetData.Merge(SResult);
            if (vwFSSTAMM.View != null)
            {
                vwFSSTAMM.View.MoveCurrentToFirst();
            }
        }
        public void ResetSetInterface()
        {
            SetMap = new SETINTEXDB();
            DataSet setResult = new DataSet();
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\FSCHGMAPINTEX.XML");
            setResult.ReadXml(filePath);
            SetMap.Merge(setResult);
            m_Data.SetCAD = SetMap;
            m_Data.SetExt = new DataSet();
        }
    
        private void btnRefreshAB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet SResult = null;
                if (dwABPOS == null)
                {
                    MessageBox.Show("请选择生产单！");
                    return;
                }
                txtMessage.Text = "";
                DataTable tbBOM = null;
                string art = "";
                string var = "";
                string color = "";
                string size = "";
                tbBOM = SetProd.Tables["FSSTUELI_EXT"];
                DataRow rm = tbBOM.NewRow();
                var qAB = from DataRow row in SetProd.ABPOS.AsEnumerable()
                          where row["ABPAUFKRS"].ToString() == dwABPOS["ABPAUFKRS"].ToString()
                          && row["ABPAUFLFD"].ToString() == dwABPOS["ABPAUFLFD"].ToString()
                          select row;
                foreach (DataRow dwABPOS_EXT in qAB)
                {
                    art = dwABPOS_EXT["ABPART"].ToString();
                    var = dwABPOS_EXT["ABPVAR"].ToString();
                    color = dwABPOS_EXT["ABPFARBE"].ToString();
                    size = dwABPOS_EXT["ABPGROE"].ToString();
                    foreach (DataRow dwBOM in tbBOM.Rows)
                    {
                        rm.ItemArray = dwBOM.ItemArray;
                        //FSSVAR	FSSFARBE	FSSGROE	FSSAUFM	FSSVERP
                        //FSSMATKZ FSSMART	FSSMVAR	FSSMFARBE FSSMGROE	 	FSSVERBR FSSVBME	FSSVBGRP	FSSVBFKT
                        //FSSVBKZ==G 用量和尺码相关
                        string FSSVAR = rm["FSSVAR"].ToString();
                        string FSSFARBE = rm["FSSFARBE"].ToString().Trim();
                        string FSSGROE = rm["FSSGROE"].ToString();
                        string FSSAUFM = rm["FSSAUFM"].ToString();
                        string FSSVERP = rm["FSSVERP"].ToString();
                        string FSSPOSTYP = rm["FSSPOSTYP"].ToString();
                        //  if (FSSVAR.Trim() != var)
                        //   continue;

                        //============ 
                        string FSSPOS = rm["FSSPOS"].ToString();
                        string FSSMATKZ = rm["FSSMATKZ"].ToString();
                        string FSSMART = rm["FSSMART"].ToString();
                        string FSSMVAR = rm["FSSMVAR"].ToString();
                        string FSSMFARBE = rm["FSSMFARBE"].ToString();
                        string FSSMGROE = rm["FSSMGROE"].ToString();
                        string FSSVERBR = rm["FSSVERBR"].ToString();
                        string FSSVBME = rm["FSSVBME"].ToString();
                        string FSSVBFKT = rm["FSSVBFKT"].ToString();
                        string FSSVERL = rm["FSSVERL"].ToString();
                        FSSVERBR = (Convert.ToDouble(FSSVERBR) * Convert.ToDouble(FSSVBFKT) * 0.000001 * (1 + Convert.ToDouble(FSSVERL) * 0.0001)).ToString();
                        rm["FSSVERBR"] = FSSVERBR;
                        //先处理颜色
                        EventDispatcher.Instance.RaiseStatus("处理原料需求" + FSSMART + FSSMVAR + FSSMFARBE, StatusEventType.RunningProgress);
                        DataRow rtp = null;
                        if (m_Data.SetExt.Tables.Contains("FSSTUELI_EXT"))
                        {
                            m_Data.SetExt.Tables["FSSTUELI_EXT"].Clear();
                        }
                        DataTable tbFS05EXT = tbBOM.Clone();
                        DataRow rFS05 = tbFS05EXT.NewRow();
                        rFS05.ItemArray = rm.ItemArray;
                        if (FSSFARBE.Trim() == "*")
                        {
                            rFS05["FSSMFARBE"] = color;
                        }
                        tbFS05EXT.Rows.Add(rFS05);
                        m_Data.SetExt.Merge(tbFS05EXT);

                        if (FSSFARBE.Trim() != "")
                        {
                            if (FSSFARBE.Trim() != "*")
                            {
                                if (FSSFARBE.Trim() != color.Trim())
                                    continue;
                            }
                        }
                        if (FSSGROE.Trim() != "")
                        {
                            if (FSSGROE.Trim() != "*")
                            {
                                if (FSSGROE.Trim() != size.Trim())
                                    continue;
                            }
                        }
                        switch (FSSMATKZ)
                        {
                            case "F"://面料 
                                SResult = m_Data.Convert2WDECK(dwABPOS_EXT);
                                foreach (DataRow rw in SResult.Tables[0].Rows)
                                {
                                    m_Data.INTEXDBC.SaveWDECK(rw);
                                }
                                break;
                            case "T":
                                SResult = m_Data.Convert2FDECKB(dwABPOS_EXT);
                                foreach (DataRow rw in SResult.Tables[0].Rows)
                                {
                                    m_Data.INTEXDBC.SaveFDECK(rw);
                                    txtMessage.Text = rw["FDAUFKRS"].ToString() + rw["FDAUFLFD"].ToString() + rw["FDAUFLFD"].ToString() + "已经插入新的原料需求!";
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
}
